package com.umpay.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.umpay.dict.retcode.MerBusiEC;
import com.umpay.entity.BaseEntity;
import com.umpay.exception.BusinessException;
/**
 * 
<p>Title:TableNameHelper</p>  
<p>Description: 用于获取水平分表的表名</p>
<p>Company: UMPAY</p>
 @author lifeng
 @date 2018年1月17日
 */
@Component
public class TableNameHelper
{
	@Autowired
	private PropertyUtil propertyUtil;
	
    public void generateOfflineTableName(BaseEntity entity,String orderDate) {
    	entity.setTableName(entity.getTableNamePrefix() + orderDate.substring(2, 6));
	    return;
	}
    
    public void generateOfflineTableNameByTrace(BaseEntity entity,String trace) {
    	entity.setTableName(entity.getTableNamePrefix() + trace.substring(0, 4));
	    return;
	}
    public void generateOfflinePreTableNameByTrace(BaseEntity entity,String trace) {
    	entity.setTableName(entity.getTableNamePrefix() + TimeUtil.calcMonth4(TimeUtil.date8().substring(0, 2)+trace.substring(0, 4),-1));
    	return;
	}
    public void generateOfflineNextTableNameByTrace(BaseEntity entity,String trace) {
    	entity.setTableName(entity.getTableNamePrefix() + TimeUtil.calcMonth4(TimeUtil.date8().substring(0, 2)+trace.substring(0, 4),1));
    	return;
	}
    
    /**
  	 * 根据orderState获取表名
  	 * @param orderDate
  	 * @return
  	 */
    public void generateTableName(BaseEntity entity,String orderDate) throws BusinessException {
		int day = 0;
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
		Calendar cal = Calendar.getInstance();
		try {
			cal.setTime(format.parse(orderDate));
		} catch (ParseException e) {
			throw new BusinessException(MerBusiEC.SYSTEM_ERR,"订单日期不合法");
		}
		day = cal.get(Calendar.MONTH);
		day = day%6;
		entity.setTableName(entity.getTableNamePrefix()+day);
		return;
	}
    /**
	 * 根据trace获取表名
	 * @param 
	 * @return
	 */
	public  void getTableNameByTrace(BaseEntity entity,String trace) throws BusinessException {
		if(StringUtils.isEmpty(trace)){
			throw new BusinessException(MerBusiEC.SYSTEM_ERR,"trace未传递");
		}
		entity.setTableName(entity.getTableNamePrefix() + trace.substring(trace.length()-1));
		return;
	}
	
	/**
	 * 根据tradeNo获取表名
	 * @param
	 * @return
	 */
	public void getTableNameByTradeNo(BaseEntity entity,String tradeNo) throws BusinessException {
		if(StringUtils.isEmpty(tradeNo)){
			throw new BusinessException(MerBusiEC.SYSTEM_ERR,"tradeNo未传递");
		}
		entity.setTableName(entity.getTableNamePrefix() + tradeNo.substring(tradeNo.length()-1));
		return;
	}
	
	public static String genOnlinePreTableNameByTrace(BaseEntity entity, String trace) {
		Integer suffix = Integer.valueOf(trace.substring(trace.length() - 1));
		String tableNamePre = "";
		if (suffix == 0) {
			tableNamePre = entity.getTableNamePrefix() + "6";
		} else {
			tableNamePre = entity.getTableNamePrefix() + (suffix - 1);
		}
		return tableNamePre;
	}

	public static String genOnlineLaterTableNameByTrace(BaseEntity entity, String trace) {
		Integer suffix = Integer.valueOf(trace.substring(trace.length() - 1));
		String tableNameLater = "";
		if (suffix == 6) {
			tableNameLater = entity.getTableNamePrefix() + "0";
		} else {
			tableNameLater = entity.getTableNamePrefix() + (suffix + 1);
		}
		return tableNameLater;
	}


    public static int getOnTableNoByOrderDate(String orderDate) {
        int day = 0;
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        Calendar cal = Calendar.getInstance();
        try {
            cal.setTime(format.parse(orderDate));
        } catch (ParseException e) {
            throw new BusinessException(MerBusiEC.SYSTEM_ERR,"订单日期不合法");
        }
        day = cal.get(Calendar.MONTH);
        return day = day%6;
    }
}
